package com.example.exam;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.exam.Entity.MenuUser;
import com.example.exam.MeunFrgment.PictureFragment;
import com.example.exam.MeunFrgment.ShopFragment;
import com.example.exam.MeunFrgment.ZhangFH;
import com.example.exam.ZhangFengHua.Introduction;
import com.example.exam.ZhangFengHua.Life_assistant;
import com.example.exam.ZhangFengHua.Linepm;
import com.example.exam.ZhangFengHua.Remember_password;
import com.example.exam.ZhangFengHua.SharedPreferences;
import com.example.exam.ZhangFengHua.Simple_fragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView main_toolbar_text;
    private Toolbar main_toolbar;
    private FrameLayout main_home_page;
    private ListView main_list;
    private LinearLayout main_open_view;
    private DrawerLayout drawer;
    private FragmentManager fm=getSupportFragmentManager();
    private FragmentTransaction ft;
    private List<MenuUser> menuUserList = new ArrayList<>();
    private MenuUser menuUser;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menuUser=new MenuUser();
        menuUser.setFragment(new PictureFragment());
        menuUser.setName("XIAOTIE图片加载");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);
        menuUser=new MenuUser();
        menuUser.setFragment(new ShopFragment());
        menuUser.setName("XIAOTIE三级联动");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);
        menuUser=new MenuUser();
        menuUser.setFragment(new ZhangFH());
        menuUser.setName("ZFH的图片加载");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);

        menuUser=new MenuUser();
        menuUser.setFragment(new Life_assistant());
        menuUser.setName("ZFH生活助手折线图");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);


        menuUser=new MenuUser();
        menuUser.setFragment(new SharedPreferences());
        menuUser.setName("ZFH轻量级储存");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);

        menuUser=new MenuUser();
        menuUser.setFragment(new Remember_password());
        menuUser.setName("ZFH记住密码");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);

        menuUser=new MenuUser();
        menuUser.setFragment(new Simple_fragment());
        menuUser.setName("ZFH简单的碎片");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);

        menuUser=new MenuUser();
        menuUser.setFragment(new Linepm());
        menuUser.setName("ZFH实时环境");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);

        menuUser=new MenuUser();
        menuUser.setFragment(new Introduction());
        menuUser.setName("ZFH的ListView");
        menuUser.setRes(R.drawable.www);
        menuUserList.add(menuUser);
        initView();
    }

    private void initView() {
        main_toolbar_text = (TextView) findViewById(R.id.main_toolbar_text);
        main_toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        main_home_page = (FrameLayout) findViewById(R.id.main_home_page);
        main_list = (ListView) findViewById(R.id.main_list);
        main_open_view = (LinearLayout) findViewById(R.id.main_open_view);
        drawer = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity.this, drawer, main_toolbar, 0, 0);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        Window window = getWindow();
        //透明导航栏
        window.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        main_list.setAdapter(new myAdapter());
        //Fragment
        main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ft=fm.beginTransaction();
                ft.replace(R.id.main_home_page,menuUserList.get(i).getFragment());
                main_toolbar_text.setText(menuUserList.get(i).getName());
                drawer.closeDrawers();
                ft.commit();


            }
        });
    }
    private class myAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return menuUserList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        private class ViewHolder {
            ImageView menu_img;
            TextView list_text;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null){
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_menu, viewGroup, false);
                holder= new ViewHolder();
                holder.menu_img = (ImageView) view.findViewById(R.id.menu_img);
                holder.list_text = (TextView) view.findViewById(R.id.list_text);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
                holder.list_text.setText(menuUserList.get(i).getName());
                holder.menu_img.setImageResource(menuUserList.get(i).getRes());
            return view;
        }

    }
}
